﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;



namespace SellerProject
{
    public class TopBasePage : System.Web.UI.Page
    {
        public TopBasePage() { }
        /// <summary>
        /// session验证
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (Session["sessionKey"] == null)
            {
                string path = Page.ResolveUrl("http://container.api.taobao.com/container?appkey=" + System.Configuration.ConfigurationManager.AppSettings["AppKey"].ToString());
                AlertAndRedirectToTop("操作超时，请重新登录！", path);
                return;
            }
            base.OnLoad(e);
        }
        //#region "otherway"
        //this.Load += new EventHandler(TopBasePage_load);
        //void TopBasePage_load(object sender, EventArgs e)
        //{
        //    if (Session["sessionKey"] == null)
        //    {
        //        string path = Page.ResolveUrl("~/sell404.aspx");
        //        AlertAndRedirectToTop("登录超时，请重新登录！", path);
        //    }
        //}
        //#endregion

        /// <summary>
        /// 弹出错误并跳转
        /// </summary>
        /// <param name="message"></param>
        /// <param name="toURL"></param>
        public static void AlertAndRedirectToTop(string message, string toURL)
        {
            string js = "<script language=javascript>alert('{0}');top.window.location.replace('{1}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, message, toURL));
        }
        public static void AlertMessage(string msg)
        {
            string runjs = "<script language=javascript>alert('{0}')</script>";
            HttpContext.Current.Response.Write(string.Format(runjs, msg));
        }

        /// <summary>
        /// 一般错误处理
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        void Page_Error(Object source, EventArgs e)
        {
            HttpUnhandledException eHttp = this.Server.GetLastError() as HttpUnhandledException;
            Exception eApp = eHttp.InnerException;//可以返回异常
            string eSouce = eApp.Source;
            string eTrace = eApp.StackTrace;
            string eTarget = eApp.TargetSite.ToString();
            string eMsg = eApp.Message;
            string redirectCode = "eSouce=" + eSouce + ";eTrace=" + eTrace + ";eTarget=" + eTarget + ";eMsg=" + eMsg;
            Server.Transfer("sell404.aspx?" + Server.UrlEncode(redirectCode));
            Server.ClearError();
            Response.End();
        }


    }
}